﻿using BigEgg.Framework.Foundation;
using BigEgg.Framework.UnitTesting;
using FMStudio.Documents.TemplateDocument.Models;
using FMStudio.Documents.TemplateDocument.Properties;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace FMStudio.Documents.TemplateDocument.Test.Models
{
    [TestClass]
    public class ProjectItemTest
    {
        [TestMethod]
        public void GeneralProjectItemTest()
        {
            ProjectItem projectItem = new ProjectItem("NewDocument");

            Assert.AreEqual("NewDocument", projectItem.Name);
            Assert.AreEqual("NewDocument", projectItem.TargetFileName);
            Assert.IsFalse(projectItem.ReplaceParameters);

            Assert.AreEqual("", projectItem.Validate());
        }

        [TestMethod]
        public void PropertiesWithNotification()
        {
            ProjectItem item = new ProjectItem("Document");

            AssertHelper.PropertyChangedEvent(item, x => x.Name, () => item.Name = "FileName");
            Assert.AreEqual("FileName", item.Name);

            AssertHelper.PropertyChangedEvent(item, x => x.TargetFileName, () => item.TargetFileName = "TargetFileName");
            Assert.AreEqual("TargetFileName", item.TargetFileName);

            AssertHelper.PropertyChangedEvent(item, x => x.ReplaceParameters, () => item.ReplaceParameters = true);
            Assert.AreEqual(true, item.ReplaceParameters);
        }

        [TestMethod]
        public void NameValidationTest()
        {
            ProjectItem project = new ProjectItem("NewDocument");

            project.Name = null;
            Assert.IsNull(project.Name);
            Assert.AreEqual(Resources.FolderNameMandatory, project.Validate("Name"));

            project.Name = "";
            Assert.AreEqual("", project.Name);
            Assert.AreEqual(Resources.FolderNameMandatory, project.Validate("Name"));

            project.Name = "SomeDocument";
            Assert.AreEqual("", project.Validate("Name"));
        }

        [TestMethod]
        public void TargetFileNameValidationTest()
        {
            ProjectItem project = new ProjectItem("NewDocument");

            project.TargetFileName = null;
            Assert.IsNull(project.TargetFileName);
            Assert.AreEqual(Resources.ProjectItemTargetFileNameMandatory, project.Validate("TargetFileName"));

            project.TargetFileName = "";
            Assert.AreEqual("", project.TargetFileName);
            Assert.AreEqual(Resources.ProjectItemTargetFileNameMandatory, project.Validate("TargetFileName"));

            project.TargetFileName = "SomeDocument";
            Assert.AreEqual("", project.Validate("Name"));
        }

        [TestMethod]
        public void SerializationTest()
        {
            ProjectItem projectItem = new ProjectItem("NewDocument.txt");
            projectItem.TargetFileName = "TargetDocument.txt";
            projectItem.ReplaceParameters = true;
            
            Assert.AreEqual("NewDocument.txt", projectItem.Name);
            Assert.AreEqual("TargetDocument.txt", projectItem.TargetFileName);
            Assert.IsTrue(projectItem.ReplaceParameters);

            XElement element = projectItem.ObjectToXElement();
            ProjectItem newProjectItem = element.XElementToObject<ProjectItem>();

            Assert.AreEqual(projectItem.Name, newProjectItem.Name);
            Assert.AreEqual(projectItem.TargetFileName, newProjectItem.TargetFileName);
            Assert.AreEqual(projectItem.ReplaceParameters, newProjectItem.ReplaceParameters);

            XElement newElement = new XElement("ProjectItem");
            newElement.SetAttributeValue("TargetFileName", "TargetDocument.txt");
            newElement.SetAttributeValue("ReplaceParameters", true);
            newElement.SetValue("NewDocument.txt");

            newProjectItem = newElement.XElementToObject<ProjectItem>();

            Assert.AreEqual(projectItem.Name, newProjectItem.Name);
            Assert.AreEqual(projectItem.TargetFileName, newProjectItem.TargetFileName);
            Assert.AreEqual(projectItem.ReplaceParameters, newProjectItem.ReplaceParameters);
        }
    }
}
